local DailyActFL = require("activity.DailyActFL")
local ActDefine = require("activity.Define")
local ArmyRiotExcel = require("excel.ArmyRiot")
local ArmyRiotMonster = ArmyRiotExcel.monster 
local Copy = require("copy.CopyL")
local Map = require("core.Map")
local ObjHuman = require("core.ObjHuman")
local Obj = require("core.Obj")
local RoleFFI = require("role.RoleFFI")
local InnerDataManagerFL = require("innerdata.InnerDataManagerFL")
local InnerDataDefine = require("innerdata.Define")
local SceneEvent = require("core.SceneEvent")
local SceneConfig = require("excel.Map").scene
local Msg = require("core.Msg")

local isInit = false
--检查是否活动怪物
function checkMonsterIsIn(monster_id)
  for k,v in pairs(ArmyRiotMonster) do
    if monster_id == k then
      return true
    end
  end
  return false
end
--得到单个怪物奖励的积分
function getReward(monster_id, scene_id)
  for k,v in pairs(ArmyRiotMonster) do
    if monster_id == k then
      if Map.scene_id_to_map_id[scene_id] == 2200 then
        return ArmyRiotMonster[monster_id].score1
      elseif Map.scene_id_to_map_id[scene_id] == 2006 then
        return ArmyRiotMonster[monster_id].score2
      elseif Map.scene_id_to_map_id[scene_id] == 2009 then
        return ArmyRiotMonster[monster_id].score3
      end
    end
  end
  return 0
end
--  Copy.isCopyBySceneID(sceneID)
-- Map.scene_id_to_map_id
function onMonsterDie(monster, attacker)  
  if not DailyActFL.onCheckActIsRunning(ActDefine.ACT_TYPE_ARMY_RIOT) then
    return
  end
  if not checkMonsterIsIn(monster.monster_id) then
    return
  end
  local team, teamLen, human
  if ArmyRiotMonster[monster.monster_id].type == 1 then --是boss
    if not attacker then
      return
    end
    human = attacker
    team = attacker.db.teamUuid 
    teamLen = attacker.db.teamUuidLen
  else --其他怪物
    local human1 = ObjHuman.onlineUuid[monster.firstHitUuid]
    if not human1 then
      return
    end
    human = human1
    team = human.db.teamUuid 
    teamLen = human.db.teamUuidLen
  end
  local integral = getReward(monster.monster_id, monster.scene_id)
  local info = {}
  info.integral = integral
  if teamLen <= 0 then
    InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_ARMY_RIOT, info)
  else
    local tmpTeam = getTeam(team,monster, teamLen)
    if #tmpTeam == 0 then
      return
    end
    for _,m in ipairs(tmpTeam) do
      local member = ObjHuman.onlineUuid[m]
      if member then
        InnerDataManagerFL.Send2WInnerData(member, InnerDataDefine.INNERDATA_TYPE_ARMY_RIOT, info)
      end
    end
  end
end

tmpTeam = tmpTeam or {}
function getTeam(team,monster,teamLen)
  --clear
  for k in pairs(tmpTeam) do 
    tmpTeam[k] = nil
  end
  
  local onlineUuid = ObjHuman.onlineUuid
  for i=0, teamLen - 1 do
    local target = onlineUuid[RoleFFI.ffi_string(team[i].uuid)]  
    if target and Obj.canSeeEachOther2(target,monster) then
      tmpTeam[#tmpTeam+1] = RoleFFI.ffi_string(team[i].uuid)
    end
  end
  
  return tmpTeam
end

--设置增加的军团贡献
function setRiotContribution(human, val)
  if DailyActFL.onCheckActIsRunning(ActDefine.ACT_TYPE_ARMY_RIOT) then
    human.db.riotContribution = human.db.riotContribution + val
  end
end

function onRunning()
  if not isInit then
    for k,v in pairs(ArmyRiotExcel.map) do
      if SceneConfig[k].logic_id == _G.logic_id then
        SceneEvent.doSceneEvent(Map.map_id_to_scene_id[k][1], v.startRef)
      end
    end
    isInit = true
  end
end
function onEnd()
  for k,v in pairs(ArmyRiotExcel.map) do
    if SceneConfig[k].logic_id == _G.logic_id and Map.map_id_to_scene_id[k][1] and Obj.scenes[Map.map_id_to_scene_id[k][1]] then
      SceneEvent.doSceneEvent(Map.map_id_to_scene_id[k][1], v.endRef)
    end
  end
  isInit = false
end
--
function sendGC_ARMY_RIOT_MONSTER_ID(human)
  local sendMsg = Msg.gc.GC_ARMY_RIOT_MONSTER_ID  
  local len = 0
  for k in pairs(ArmyRiotMonster) do
    len = len + 1
    sendMsg.monsterId[len] = k
  end
  sendMsg.monsterId[0] = len 
  Msg.send(sendMsg, human.fd)
end
DailyActFL.onRegisterCallBack(ActDefine.ACT_TYPE_ARMY_RIOT, nil, onRunning, onEnd, nil)
